// Package routers
// @Author: zhangdi
// @File: router
// @Version: 1.0.0
// @Date: 2023/6/21 15:38
package router

import (
	"github.com/gin-gonic/gin"

	"server/pkg/ginp"
)

// Routers 存放所有的路由定义
var Routers = []ItemInfo{}

func Register(r *gin.Engine) {
	//跨域设置
	r.Use(CORSMiddleware())

	//登录鉴权检验
	r.Use(ginp.ConvHandler(AuthorizationCheck))
	//权限验证
	r.Use(ginp.ConvHandler(permissionCheck))

	//路由注册
	registerRouter(r, Routers)

	//注册公共视图路由
	//registerRouter(r, PublicViewRoutes)

}

// 注册路由
func registerRouter(r *gin.Engine, Items []ItemInfo) {
	for _, item := range Items {
		switch item.HttpType {
		case HttpPost:
			r.POST(item.Path, item.Handlers)
		case HttpGet:
			r.GET(item.Path, item.Handlers)
		case HttpPut:
			r.PUT(item.Path, item.Handlers)
		case HttpPatch:
			r.PATCH(item.Path, item.Handlers)
		case HttpAny: //任意请求方式都行
			r.Any(item.Path, item.Handlers)
		}
	}
}

// AppendRouter 向现有的路由新增
func AppendRouter(item ItemInfo) {
	Routers = append(Routers, item)
}
